import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        // imgBaseUrl: 'https://test.utouxci.cn/wxImage/',
        imgBaseUrl: 'https://zxff.shinuokeji.net/wxImage/',
        // wssBaseUrl: 'wss://test.utouxci.cn/wss',
        wssBaseUrl: 'wss://zxff.shinuokeji.net/wss',
        // baseUrl: 'https://test.utouxci.cn/app/index.php?i=1&c=entry&m=ewei_shopv2&do=mobile&r=',
        baseUrl: 'https://zxff.shinuokeji.net/app/index.php?i=1&c=entry&m=ewei_shopv2&do=mobile&r=',
        userAvatar: '',
        loginStatus: true, // 是否退出websocket登录
        publicSocketTask: '', // 全局的SocketTask
        identity: 'buyer', // 身份，客户:buyer、律师: seller
        platform: '', // 判断平台，ios，Android
        connectTimer: 0, // 重连定时器
        openDoc: false, // 是否打开文件
        preview: false, // 是否预览图片
        navbarHeight: 0, // 自定义导航栏高度
    },
    mutations: {
        setName(state, value) {
            state.name = value
        },
        setStatus(state, value){
            state.loginStatus = value
        },
        setPlatform(state, value){
            state.platform = value
        },
        setPreview(state, value){
            state.preview = value
        },
    },
    actions: {
        globalSocket(){
            const that = this  // store
            uni.onSocketOpen(function(res) {
                console.log("WebSocket连接已打开！");
                // 发送用户信息给服务端
                const data = {
                    type: 'storage_users',
                    uid: uni.getStorageSync('uid'),
                    identity: that.state.identity
                }
                that.dispatch("sendMsg", data);
                if(that.state.platform == 'android'){
                    clearInterval(that.state.connectTimer)
                }else{
                    clearTimeout(that.state.connectTimer)
                }
                uni.hideLoading();
            });
            uni.onSocketClose(function(res) {
                console.log('断开了22222');
                if(that.state.platform == 'android'){
                  // 安卓要用setInterval，否则只执行一次重连
                  that.state.connectTimer = setInterval(() => {
                    console.log('重连');
                    uni.connectSocket({
                      url: that.state.wssBaseUrl,
                      success(){
                        uni.showLoading({
                          title: '拼命重连中...'
                        });
                      }
                    });
                  }, 1000)
                }else{
                  that.state.connectTimer = setTimeout(() => {
                    console.log('重连1');
                    uni.connectSocket({
                      url: that.state.wssBaseUrl,
                      success(){
                        uni.showLoading({
                          title: '拼命重连中...'
                        });
                      }
                    });
                  }, 1000)
                }
              });
        },
        callName({ commit }, value) {
            commit('setName', value)
        },
        modal({ commit }, content) {
            return new Promise((resolve, reject) => {
                uni.showModal({
                    title: "提示",
                    content,
                    success: function (res) {
                        if (res.confirm) {
                            resolve(true)
                        } else if (res.cancel) {
                            reject(false)
                        }
                    }
                });
            })
        },
        // 提示登录的弹窗
        login({ commit }, msg = '请登录再操作!' ) {
            uni.removeStorageSync('token');
            uni.showModal({
                title: "提示",
                content: msg,
                success: function (res) {
                    if (res.confirm) {
                        uni.switchTab({
                            url: "/pages/my/my"
                        });
                    } else if (res.cancel) {
                        uni.navigateBack({
                            delta: 1
                        });
                    }
                }
            });
        },
        sendMsg({ commit }, data) {
            return new Promise((resolve, reject) => {
                console.log(data);
                uni.sendSocketMessage({
                    data: JSON.stringify(data),
                    success() {
                        console.log('发送成功');
                        resolve(true)
                    },
                    fail(err){
                        reject(err)
                    }
                });
            })
        },
        reSend({ commit }, data){
            
        }
    }
})